package com.wzg.security;

import java.io.*;
import java.util.Arrays;

/**
 * @author henryGui
 * @version V1.0
 * @Title: keytool
 * @Package com.wzg.security
 * @Description: TODO
 * @date 2018-07-09 下午3:56
 */
public class Execute {
    public static void command(String command){
        try
        {
            Process process=new ProcessBuilder(Arrays.asList(command.split(" "))).start();
            //标准输出流
            final OutputStream out = process.getOutputStream();
            //标准输入流
            final InputStream in = process.getInputStream();
            //标准错误输入流 二次确认信息在在此获取
            final InputStream errin = process.getErrorStream();

            Runnable rin1 = new Runnable() {
                public void run() {
                    try{
                        BufferedReader result= new BufferedReader(new InputStreamReader(in));
                        String s = null;
                        while((s=result.readLine())!=null){
                            System.out.println(s);
                        }

                    }catch(Exception e){
                        e.printStackTrace();
                    }

                }
            };

            Runnable rin2 = new Runnable() {
                public void run() {
                    try{
                        //不适用BufferedReader读取一行
                        byte[] b = new byte[4096];
                        for(int n;(n = errin.read(b))!= -1;)   {
                            String str = new   String(b, 0, n);
                            System.err.println(str);
                            //确认信息
                            if("信任这个认证？ [否]：  ".equals(str)){
                                BufferedWriter  bw =  new BufferedWriter(new OutputStreamWriter(out));
                                bw.write("y");
                                bw.newLine();
                                bw.flush();
                                bw.close();
                            }
                        }

                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            };

            Thread t1 = new Thread(rin1);
            Thread t2 = new Thread(rin2);

            t1.start();
            t2.start();

            Thread.sleep(3000);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        //导入
        //command("keytool -import -keystore \"E:/Program Files/Java/jdk1.6.0_45/jre/lib/security/cacerts\" -storepass changeit -keypass changeit -alias bocommca -file C:/bocommjava/cert/root.cer ");
        //删除
        //command("keytool -delete -alias bocommca -keystore \"E:/Program Files/Java/jdk1.6.0_45/jre/lib/security/cacerts\"  -storepass changeit ");
    }
}
